Factory Design Pattern Code_New
interface shape{ public double getArea(); public double getPerimeter(); } class square implements shape{ double l; public square(double l){ this.l = l; } public double getArea(){ return l * l; } public double getPerimeter(){ return 4 * l; } } class circle implements shape{ double r,pi = 3.14; public circle(double r){ this.r = r; } public double getArea(){ return pi*r*r; } public double getPerimeter(){ return 2 * 3.14* r; } } class shapeFactory{ public static shape getObject(String type,double val){ if(type.equals("square")) return new square(val); else if(type.equals("circle")) return new circle(val); return new square(3); } } class shapeFactoryDemo{ public static void main(String[] aa){ shape obj = shapeFactory.getObject("square",4); System.out.println("Area of square: "+obj.getArea()); obj = shapeFactory.getObject("circle",6); System.out.println("Area of circle: "+obj.getArea()+"Perimeter: "+obj.getPerimeter()); } }